﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XiAnAirport.DAL;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class MenuFOptionBUS
    {
        public ResponseMessage AddMenu(Menu menuinfo)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = da.AddMenu(menuinfo);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage EditMenu(Menu menuinfo)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = da.EditMenu(menuinfo);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelMenu(int menuid)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = string.Empty;
            result = da.DelMenu(menuid);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage QueryMenu(Menu menuinfo)
        {
            ResponseMessage message = new ResponseMessage();

            MenuFOptionDAL da = new MenuFOptionDAL();
            List<Menu> content = da.QueryMenu(menuinfo);

            message.errcode = 0;
            message.errmsg = "";
            message.data = content;
            message.total = da.QueryMenuCount(menuinfo);

            return message;
        }

        public ResponseMessage AddFOption(FOption foptioninfo)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = da.AddFOption(foptioninfo);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage EditFOption(FOption foptioninfo)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = da.EditFOption(foptioninfo);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelFOption(int foptionid)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = da.DelFOption(foptionid);

            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage QueryMenuByRole(UserInfo userinfo)
        {
            ResponseMessage message = new ResponseMessage();

            MenuFOptionDAL da = new MenuFOptionDAL();
            List<Menu> content = da.QueryMenuByRole(userinfo);

            message.errcode = 0;
            message.errmsg = "";
            message.data = content;
            message.total = da.QueryMenuByRoleCount(userinfo);

            return message;
        }

        public ResponseMessage LoadMenu(int menuid)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            ResponseMessage message = new ResponseMessage();
            Menu content = da.LoadMenu(menuid);

            if (content != null)
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "未查询到相关信息";
                message.data = null;
                message.total = 0;
            }

            return message;
        }

        public ResponseMessage LoadFOption(int optionid)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            ResponseMessage message = new ResponseMessage();
            FOption content = da.LoadFOption(optionid);

            if (content != null)
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "未查询到相关信息";
                message.data = null;
                message.total = 0;
            }

            return message;
        }

        public ResponseMessage QueryAllOption(UserInfo userinfo)
        {
            ResponseMessage message = new ResponseMessage();

            List<MenuFunc> menuFuncslist = new List<MenuFunc>();
            MenuFOptionDAL da = new MenuFOptionDAL();
            Menu menuinfo = new Menu();
            List<Menu> content = da.QueryMenuByRole(userinfo);

            foreach (var item in content)
            {
                MenuFunc menuFunc = new MenuFunc();
                menuFunc.menuName = item.name.ToString();
                menuFunc.children = da.QueryFOptionByMenuID(item.id);
                menuFunc.id = item.id;
                menuFunc.parentId = item.parentId;
                menuFuncslist.Add(menuFunc);
            }
            if (menuFuncslist != null)
            {
                message.errcode = 0;
                message.data = menuFuncslist;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.data = "";
                message.errmsg = "";
            }


            return message;
        }

        public ResponseMessage QueryAllMenuOption()
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionDAL da = new MenuFOptionDAL();
            Menu menu = new Menu();
            List<Menu> maincontent = da.QueryMenu(menu);

            if (maincontent != null)
            {
                foreach (var mainitem in maincontent)
                {
                    Menu menu1 = new Menu();
                    menu1.parentId = mainitem.id;
                    List<Menu> childcontent = new List<Menu>();
                    childcontent = da.QueryMenu(menu1);
                    mainitem.Child = childcontent;
                    if (childcontent != null)
                    {
                        foreach (var childitem in childcontent)
                        {
                            childitem.Option = da.QueryFOptionByMenuID(childitem.id);
                        }
                    }
                }
                message.errcode = 0;
                message.data = maincontent;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.data = "";
                message.errmsg = "";
            }

            return message;
        }

        public ResponseMessage QueryOptionByUserID(int userid, string Url)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionDAL da = new MenuFOptionDAL();

            List<FOption> content = da.QueryOptionByUserID(userid, Url);
            message.errcode = 0;
            message.data = content;
            message.total = content.Count;
            message.errmsg = "";

            return message;
        }

        public ResponseMessage QueryOptionByMenuID(int menuid)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionDAL da = new MenuFOptionDAL();

            List<FOption> content = da.QueryFOptionByMenuID(menuid);
            message.errcode = 0;
            message.data = content;
            message.total = content.Count;
            message.errmsg = "";

            return message;
        }

        public ResponseMessage BatchDelMenu(string ids)
        {
            MenuFOptionDAL da = new MenuFOptionDAL();
            string result = string.Empty;

            result = da.BatchDelMenu(ids);
            ResponseMessage message = new ResponseMessage();
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
